//
// gcssai_logs.cpp
// gcs
// Grand Central Station, controlling process of the Protea Application Server / Business Logic Transactions for SAI Logging management
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
// 
// Please send correspondence to:
// 
// theproteaproject@gmail.com
//

// gcssai.cpp
//
// Contains the SAI transactions, compiled into GCS
//

#include "../SDK/ToolsInterface.h"
#include "../AppHandler/parms_xml.h"
#include "gcs.h"



//@ SEC 2
//@ CHA Review Admin Logs
//@ COD ReviewAdminLogs
//@ NUM 1
TRANSACTION_CODE( XCGCS::ReviewAdminLogs )
{
	XCBuffer* detail = NULL;

	START_TXN( ReviewLogs );

		char data[ 255 ];
		if (XCFileExists( "admin.log" )) {
			int child = txnvars.buffer->GetChildFormat();
			if ( child ) {
				//@ Create the child buffer
				detail = CreateBuffer( child, false );

				fstream logs;
				logs.open( "admin.log", ios::in );
				while (!logs.eof()) {
					//@ Clear the detail data
					logs.getline( data, 2048 );

					//@ Separate the data
					if (*data) {
						char* start = data;
						char* end;

						start = strstr( start, "TMS:" );
						if (start) {
							start += 4;
							end = strchr( start, ',' );
							*end = 0;
							detail->GetField( FLD_ERR_TIME )->SetString( start );
							start = end + 1;
						};

						start = strstr( start, "ERR#: " );
						if (start) {
							start += 6;
							end = strchr( start, ',' );
							*end = 0;
							detail->GetField( FLD_ERR_NUMBER )->SetString( start );
							start = end + 1;
						};

						start = strstr( start, "PID#" );
						if (start) {
							start += 4;
							end = strchr( start, ',' );
							*end = 0;
							detail->GetField( FLD_ERR_PID )->SetString( start );
							start = end + 1;
						};

						start = strstr( start, "MSG:" );
						if (start) {
							start += 4;
							detail->GetField( FLD_ERR_DESCRIPTION )->SetString( start );
						};

						//@ Send the response
						TransResponse( txnvars.result, detail, TRANS_DETAIL_TYPE );
					};
				}; // while !eof
				logs.close();
			};	// if child
		} else {
			// File doesn't exist
			txnvars.Errors->Add( ERR_NO_ADMIN_LOG, SEV_CRITICAL );
		};
	
		txnvars.result->SetSuccess( true );

		CLEANUP_TXN_1();
			CleanupObjects( 1, &detail );
		CLEANUP_TXN_2();
			CleanupObjects( 1, &detail );
		CLEANUP_TXN_3();
			CleanupObjects( 1, &detail );
	END_TXN();
};
